package com.itheima.springboottlias.controller;

import com.itheima.springboottlias.pojo.Emp;
import com.itheima.springboottlias.pojo.PageBean;
import com.itheima.springboottlias.service.EmpService;
import com.itheima.springboottlias.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

/**
 * @author Taotao
 * @date 2023/7/15 21:45
 * @desciption: 员工
 */
@RestController
@RequestMapping("/emps")
public class EmpController {
    @Autowired
    private EmpService empService;

    /**
     * 分页条件查询
     * @param name
     * @param gender
     * @param begin
     * @param end
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping
    //@SysLog
    public Result page(String name, Integer gender, @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize) {
        PageBean pageBean = empService.page(name, gender, begin, end, page, pageSize);
        return Result.success(pageBean);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @DeleteMapping("{ids}")
    public Result deleteById(@PathVariable List<Integer> ids){
        empService.deleteById(ids);
        return Result.success();
    }

    /**
     * 新增员工
     * @param emp
     * @return
     */
    @PostMapping
    public Result addEmp(@RequestBody Emp emp){
        empService.addEmp(emp);
        return Result.success();
    }

    /**
     * 根据id查找员工
     * @return
     */
    @GetMapping("/{id}")
    public Result selectById(@PathVariable Integer id){
        Emp emp = empService.selectById(id);
        return Result.success(emp);
    }

    /**
     * 根据id修改员工
     */
    @PutMapping
    public  Result updateById(@RequestBody Emp emp){
        empService.updateById(emp);
        return Result.success();
    }
}
